#include "AppSettings.h"

#include "core/AppDirectory.h"
namespace {
const QString AppSettingsFilename = "app.settings.json";
}

AppSettings::AppSettings() {}

AppSettings& AppSettings::get()
{
    static AppSettings instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppFile(AppSettingsFilename));
        instance.load();
    }
    return instance;
}

void AppSettings::fromJson(const QJsonObject& json, QStringList& errorList)
{

    appDocDir_ = json["appDocDir"].toString(appDocDir_);
    pclRepository_ = json["pclRepository"].toString(pclRepository_);
    pclDataRepository_ = json["pclDataRepository"].toString(pclDataRepository_);
}

QJsonObject AppSettings::toJson() const
{
    QJsonObject json = BaseDocument::toJson();

    json.insert("appDocDir", QJsonValue(this->appDocDir_));
    json.insert("pclRepository", QJsonValue(this->pclRepository_));
    json.insert("pclDataRepository", QJsonValue(this->pclDataRepository_));

    postToJosn();
    return json;
}

void AppSettings::postToJosn() const
{
    AppDirectory::get().setDocDir(this->appDocDir_);
}
